package com.kavin.collection.datastructure;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.util.Objects;

/**
 * @ClassName DoubleLinkedList2
 * @Description
 * @Author kavin
 * @Date 2022/8/16 9:44
 * @Version 1.0
 */
@Slf4j
public class DoubleLinkedList2 {
    class Node {

        private int value;
        private Node pre;
        private Node next;

        @Override
        public String toString() {
            return "Node:" + "value=" + value ;
        }

        Node() {
        }

        Node(int value) {
            this.value = value;
        }


    }

    private Node first;
    private Node last;
    private int size;

    public boolean isEmpty() {
        return size == 0 ? true : false;
    }

    public void addFirst(Node node) {
        if (isEmpty()) {
            last = node;
        } else {
            node.next = first;
            first.pre = node;
        }
        first = node;
        size++;
    }

    public void addLast(Node node) {
        if (isEmpty()) {
            first = node;
        } else {
            node.pre = last;
            last.next = node;
        }
        last = node;
        size++;
    }

    public Node addBeforeNode(int value, Node node) {

        if (isEmpty()) {
            log.info("未找到对应的记录值");
            return null;
        }
        Node temp = first;
        while (Objects.nonNull(temp)) {
            if (temp.value == value) {
                if (Objects.isNull(temp.pre)) {
                    temp.pre = node;
                    node.next = temp;
                    first = node;
                    size++;
                    return node;
                } else {
                    node.next = temp;
                    node.pre = temp.pre;
                    temp.pre.next = node;
                    temp.pre = node;
                    size++;
                    return node;
                }
            }
            temp = temp.next;
        }
        return null;
    }

    public Node addAfterNode(int value, Node node) {
        if (isEmpty()) {
            log.info("未找到对应的记录值");
            return null;
        }
        Node temp = first;
        while (Objects.nonNull(temp)) {
            if (temp.value == value) {
                if (Objects.isNull(temp.next)) {
                    node.pre = temp;
                    temp.next = node;
                    last = node;
                    size++;
                    return node;
                } else {
                    Node next = temp.next;
                    node.pre = temp;
                    node.next = next;
                    temp.next = node;
                    next.pre = node;
                    size++;
                    return node;
                }
            }
            temp = temp.next;
        }
        return null;
    }

    public void deleteFirst() {
        if(isEmpty()){
            return;
        }
        if(first.next == null){
           first = null;
           last  = null;
           size -- ;
        }else {
            Node firstNode = first.next ;
            first = firstNode;
            firstNode.pre = null;
            size --;
        }

    }

    public void deleteLast() {
        if(isEmpty()){
            return;
        }
        if(last.pre == null){
            last = null;
            first = null;
            size --;
            return;
        }
        Node lastNode = last.pre;
        last = lastNode;
        lastNode.next = null;
        size --;
    }

    public void deleteByValue(int value) {
        if(isEmpty()){
            return;
        }

        Node temp = first;
        while (Objects.nonNull(temp)){
            if(temp.value == value){
                if(temp.pre == null){ //first
                    Node firstNode = temp.next;
                    firstNode.pre = null;
                    first = firstNode;
                    size-- ;
                    return;
                }

                if(temp.next ==null){  //last
                    Node lastNode = temp.pre;
                    lastNode.next = null;
                    last = lastNode;
                    size --;
                    return;
                }

                Node preNode =  temp.pre;
                Node nextNode  = temp.next;
                temp = null;
                preNode.next = nextNode;
                nextNode.pre= preNode;

                size --;
            }

            temp = temp.next;
        }

    }


    public static void bubble_v2(int[] arr) {
        for(int i = 0 ; i < arr.length - 1 ; i ++){
            for(int j = 0 ; j < arr.length-1-i;j++){
                if(arr[j] > arr[j+1]){
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }
        for (int i = 0 ; i < arr.length; i++ ){
            System.out.print(arr[i] + ",");
        }

    }




        //打印链表
    public void printDoubleLinkedList(){
        Node tmp = first;
        while(tmp != null){
            System.out.println(tmp);
            tmp = tmp.next;
        }
    }
    @Test
    public void test() {
        int[] arr = {9,4,6,1,2,6,7};
        bubble_v2(arr);



        addLast(new Node(3));
        addLast(new Node(4));
        addLast(new Node(5));
        deleteByValue(5);
        printDoubleLinkedList();
        //Node node = addAfterNode(5, new Node(6));
        //System.out.println(node);
    }
}